package com.project.webservice.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.webservice.dto.ProGetRidStationDto;
import com.project.webservice.dto.SelectRfidStatDto;

import com.project.webservice.entity.JgGjxl;
import com.project.webservice.entity.RfidBgGjxl;
import com.project.webservice.mapper.RfidBgGjxlMapper;
import com.project.webservice.service.JgGjxlService;
import com.project.webservice.service.RfidBgGjxlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service

public class RfidBgGjxlServiceImpl extends ServiceImpl<RfidBgGjxlMapper, RfidBgGjxl> implements RfidBgGjxlService {
    @Resource
    private JgGjxlService jgGjxlService;
    @Override
    @DS("rfid")
    public List<Map<String, String>> proGetBgGjx(List<String> yhids) {
        List<Map<String, String>> mapList = new ArrayList<>();
        List<Integer> jgxlids = getJgGjxlIds(yhids);
        //获取机构公交线路id为key的map
        Map<Integer,JgGjxl> jgGjxlViewMap = getJgGjxlViewList(yhids);
        if(jgxlids != null && !jgxlids.isEmpty()){
            //查找rfid公交线路
            List<RfidBgGjxl> list = super.list(Wrappers.<RfidBgGjxl>lambdaQuery().in(RfidBgGjxl::getJgxlid,jgxlids));
            if(list != null && !list.isEmpty()){
                list.forEach(r->{
                    Map<String, String> map = new HashMap<>();
                    map.put("RID_UID",r.getBgno());
                    map.put("STATION_TYPE",r.getZdlx() != null?r.getZdlx().toString():null);
                    //根据jgxlid获取对象，获取xmbm
                    map.put("LINE_STANDARD_CODE",jgGjxlViewMap.get(r.getJgxlid()) != null?jgGjxlViewMap.get(r.getJgxlid()).getXlbm():null);
                    mapList.add(map);
                });
            }
        }
        return mapList;
    }

    /**
     * 读取基站与站点关系表
     * @return
     */
    public List<SelectRfidStatDto> SelectRfidStat(){
        return super.baseMapper.selectRfidStat();
    }
    /**
     * GetRIDSTATION
     * @param bgno
     * @return
     */
    public List<ProGetRidStationDto> proGetRidStation(String bgno){
        log.info("proGetRidStation:{}",bgno);
        return super.baseMapper.proGetRidStation(bgno);
    }
    /**
     * 获取机构公交线路map
     * @param yhids
     * @return
     */
    private Map<Integer,JgGjxl> getJgGjxlViewList(List<String> yhids) {
        List<JgGjxl> list =  jgGjxlService.searchByListByYhids(yhids);
        return list != null && !list.isEmpty()?list.stream().collect(
                Collectors.toMap(JgGjxl::getJgxlid,j->j)):new HashMap<>();

    }
    /**
     * 获取机构公交线路id
     * @param yhids
     * @return
     */
    private List<Integer> getJgGjxlIds(List<String> yhids) {
        List<JgGjxl> list =  jgGjxlService.searchByListByYhids(yhids);
        return list != null && !list.isEmpty()?list.stream().map(JgGjxl::getJgxlid).collect(Collectors.toList()):null;
    }


}
